﻿using ExamWeek01.Domain;
using ExamWeek01.Repository;
using ExamWeek01.Service.IService;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExamWeek01.Service.Service
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class RoleService : BaseService<Role>, IRoleService
    {
        /// <summary>
        /// 上下文
        /// </summary>
        UserDbContext db;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<RoleService> logger;
        /// <summary>
        /// 方法
        /// </summary>
        /// <param name="db">上下文</param>
        public RoleService(UserDbContext db, ILogger<RoleService> logger) : base(db)
        {
            this.db = db;
            this.logger = logger;
        }
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="roleName">角色名</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页码</param>
        /// <param name="totalCount">总共</param>
        /// <param name="listCount">总数量</param>
        /// <returns>列表显示</returns>
        public List<Role> GetRoles(string roleName, int pageIndex, int pageSize, out int totalCount, out int listCount)
        {
            try
            {
                var list = this.GetAll().Where(x => x.IsDel == false).ToList();
                if (!string.IsNullOrEmpty(roleName))
                {
                    list = list.Where(x => x.RName.Contains(roleName)).ToList();
                }
                totalCount = list.Count;
                listCount = (int)Math.Ceiling((pageIndex * 1.0) / pageSize);
                list = list.OrderBy(x => x.RId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                return list;
            }
            catch (Exception ex)
            {
                logger.LogError("获取角色列表出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>返回受影响行数</returns>
        public int AddRole(Role role)
        {
            try
            {

                role.CreateTime = DateTime.Now;
                role.UpdateTime = DateTime.Now;
                role.IsDel = false;
                db.Add(role);
                return db.SaveChanges();

            }
            catch (Exception ex)
            {
                logger.LogError("添加角色信息错误" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="rId">角色id</param>
        /// <returns></returns>
        public int DeleteRole(int rId)
        {
            try
            {
                var entity = db.Role.Find(rId);
                var roleentity = db.UserRole.Find(entity.RId);
                if (roleentity == null)
                {
                    entity.IsDel = true;
                    db.Update(entity);
                }
                else
                {
                    return -1;
                }
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.LogError("删除角色信息错误" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 反填角色实体
        /// </summary>
        /// <param name="rId">角色id</param>
        /// <returns>实体</returns>
        public Role GetRoleId(int rId)
        {
            try
            {
                var entity = db.Role.Find(rId);
                return entity;
            }
            catch (Exception ex)
            {
                logger.LogError("获取角色实体失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="role">角色</param>
        /// <returns>返回受影响行数</returns>
        public int UpdRole(Role role)
        {
            try
            {
                var entity = this.GetRoleId(role.RId);
                db.Update(entity);
                return db.SaveChanges();

            }
            catch (Exception ex)
            {
                logger.LogError("修改角色信息错误" + ex.Message);
                throw;
            }
        }
       
    }
}
